package com.spike.servlet;

import com.spike.model.Dept;
import com.spike.service.EmpService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/empList")
public class EmpListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
//        查询全部员工信息
        String page = req.getParameter("page");
        if (page == null) {
            page = "1";
        }
        int pa = Integer.parseInt(page);
        int p = 10*(pa-1)+7;
        String name = req.getParameter("name");
        String minSalary = req.getParameter("minSalary");
        String maxSalary = req.getParameter("maxSalary");
        try {
            List<Dept> list = new EmpService().findAll(name,minSalary,maxSalary,p,10);
            req.setAttribute("list", list);
            req.setAttribute("page", page);
            req.setAttribute("minSalary", minSalary);
            req.setAttribute("maxSalary", maxSalary);
            req.getRequestDispatcher("/WEB-INF/jsp/empList.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
